package UIFree.controls
{
	import Classes.CLoader;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	import UIFree.uif_FXComponent;
	import UIFree.uif_component;

	public class uif_FXSource extends uif_FXComponent {
		protected var _source: CLoader;
		protected var _imageSize: Point;
		public function uif_FXSource(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_imageSize = new Point();
			var inner: Rectangle = getInner();
			_source = new CLoader(this, '', doLoad, inner.x, inner.y, new LoaderContext(true));
		}
		
		protected function doLoad(e: Event): void {
			_imageSize = new Point(_source.width, _source.height);
			centeredSource();
		}
		
		protected function centeredSource(): void {
			if (_source) {
				var coordinates: Rectangle = EnterHere(_imageSize);
				_source.x 		= coordinates.x;
				_source.y 		= coordinates.y;
				_source.width 	= coordinates.width;
				_source.height 	= coordinates.height;
			}
		}
				
		public function get sourceScale(): Number {
			return _source.scaleX;
		}
		
		public function get source(): CLoader {
			return _source;
		}
		
		public function get imageSize(): Point {
			return _imageSize;
		}
		
		public function loadSource(url: String): void {
			if (url) {
				if (_source.contentLoaderInfo.bytesLoaded > 0) _source.unload();
				_source.load(new URLRequest(url), new LoaderContext(true));
			}
		}
	}
}